<?php
/**
 * 购物车控制器
 *
 * @package    m
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  m
 * @extends  Controller
 */

class Controller_Trolley extends Controller_BaseController{

	public function before(){
    	parent::before();
  	}

	/**
	* 购物车商品添加移除操作
	* @param $goods_id int 商品ID
	* @param $num int 商品数量
	* @return String 
	*/
	public function action_index(){

		$data = \Input::post();

		if ( ! isset($data['goods_id']) || ! $data['goods_id']) {
			if(\Input::is_ajax()){
				die(json_encode(array('status' => 'err', 'msg' => '无效的参数', 'errcode' => 10)));
			}
		}

		//数据补充，便于程序判断
		$data['color'] = \Input::post('color', '');
		$data['size'] = \Input::post('size', '');

		$params = array();
		if(\Input::get('action', false) == 'add'){
			$this->trolley_cookie($data);
		}else if(\Input::get('action', false) == 'remove'){
			$this->trolley_cookie_remove($data);
		}

		if(\Input::is_ajax()){
			die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
		}		
	}

	/**
	* 查看购物车中的商品
	* @return Response
	*/
	public function action_list(){
		$account = \Session::get('WXAccount');
		
		//获取Cookie中的购物车信息
		$trolley = \Cookie::get("trolley-{$account->seller_id}-{$account->id}");
		if( ! $trolley){
			$trolley = serialize(array());
		}			

		$items = unserialize($trolley);
		die(json_encode(array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0, 'data' => $items)));
	}

	/**
	* 判断购物车是否为空
	* @param $data Array 商品数据(商品ID、颜色、尺寸)
	* @return Boolean
	*/
	public function action_empty(){
		$account = \Session::get('WXAccount');
		$items = array();

		$trolley = \Cookie::get("trolley-{$account->seller_id}-{$account->id}");
		if($trolley){
			$items = unserialize($trolley);
		}

		if(isset($items) && $items){
			if(count($items) > 0) {
				die(json_encode(array('status' => 'succ', 'msg' => '')));
			}
		}
		die(json_encode(array('status' => 'err', 'msg' => '')));
	}

	/**
	* 数据库版购物车
	* @param $data Array 商品数据(商品ID、颜色、尺寸)
	* @return Boolean
	*/
	private function trolley_db($data){
		$account = \Session::get('WXAccount');
		$items = array();
	}

	/**
	* Coockie版购物车
	* @param $data Array 商品数据(商品ID、颜色、尺寸)
	* @return Boolean
	*/
	private function trolley_cookie($data){
		$account = \Session::get('WXAccount');
		
		//获取Cookie中的购物车信息
		$trolley = \Cookie::get("trolley-{$account->seller_id}-{$account->id}");
		if( ! $trolley){
			$trolley = serialize(array());
		}			

		$items = unserialize($trolley);
		$flag = 0;
		//判断Cookie中是否存在该商品
		foreach ($items as $key => $value) {
			if($data['goods_id'] == $value['goods_id']
				&& $data['color'] == $value['color']
				&& $data['size'] == $value['size']){
				$items[$key]['num'] = isset($data['num']) ? $data['num'] : 1;
				$flag = 1;
				break;
			}
		}

		//新增商品至购物车
		if( ! $flag){
			array_push($items, $data);
		}		

		return \Cookie::set("trolley-{$account->seller_id}-{$account->id}", serialize($items), 60 * 60 * 24 * 365);
	}

	/**
	* Coockie版购物车——移除商品
	* @param $data Array 商品数据(商品ID、颜色、尺寸)
	* @return Boolean
	*/
	private function trolley_cookie_remove($data){
		$account = \Session::get('WXAccount');
		$items = array();

		//获取Cookie中的购物车信息
		$trolley = \Cookie::get("trolley-{$account->seller_id}-{$account->id}");
		if($trolley){
			$items = unserialize($trolley);
		}

		//判断Cookie中是否存在该商品
		foreach ($items as $key => $value) {
			if($data['goods_id'] == $value['goods_id']
				&& $data['color'] == $value['color']
				&& $data['size'] == $value['size']){
				unset($items[$key]);
				break;
			}
		}

		return \Cookie::set("trolley-{$account->seller_id}-{$account->id}", serialize($items), 60 * 60 * 24 * 365);
	}
}